﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Components = GMAC.ReportingSystem.Business.Components;
using Entities = GMAC.ReportingSystem.Business.Entities;

namespace GMAC.ReportingSystem
{
    public partial class ChangePassword : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null)
            {
                System.Web.Security.FormsAuthentication.RedirectToLoginPage();
                return;
            }

            if (!this.IsPostBack)
            {

            }
        }

        protected void btnAcceptChange_Click(object sender, ImageClickEventArgs e)
        {
            if (this.IsValid)
            {
                Components.UserComponent bcUser = new GMAC.ReportingSystem.Business.Components.UserComponent();
                Entities.User beUser = (Entities.User)Session["User"];
                Entities.User beUserOld = null;
                if (beUser != null)
                {
                    try
                    {
                        beUserOld = bcUser.Login(beUser.Login, this.txtPasswordOld.Text);
                    }
                    catch (Exception ex)
                    {
                        this.wucMessage.ShowMessage(UserControls.MessageType.Error, "An error has occured changing your password.", null);
                        this.popupExtenderMessage.Show();
                        this.btnAccept.Focus();
                    }
                    if (beUserOld != null)
                    {
                        try
                        {
                            beUser.Password = this.txtPassword.Text;
                            beUser.MustChangePassword = false;
                            bcUser.ChangePassword(beUser);
                        }
                        catch (Exception ex)
                        {
                            this.wucMessage.ShowMessage(UserControls.MessageType.Error, "An error has occured changing your password.", null);
                            this.popupExtenderMessage.Show();
                            this.btnAccept.Focus();
                        }
                        //FormsAuthentication.RedirectFromLoginPage(beUser.Login, true);
                        Response.Redirect("Home.aspx");
                    }
                    else
                    {
                        this.wucMessage.ShowMessage(UserControls.MessageType.Error, "Invalid current password.", null);
                        this.popupExtenderMessage.Show();
                        this.btnAccept.Focus();
                    }
                }
            }
        }
    }
}
